<?php
/**
 * Create a new Survey - Main Screen
 * 
 * @author	Xavier VILAIN
 * @package Nawa\Survey
 * @license #license-nawa-survey#
 * 
 */

use Fox\Ressources\Script;
use Fox\Behaviors;
use Nawa\Survey\Question;
use Nawa\Messages;
use Fox\Html\Block;
use Fox\Html\Button;
use Fox\Generator;
use Nawa\Survey;
use Fox\Html\Screen;

$xview = Xview::instance();
$survey = $xview->component('survey','object');
$data = $survey->getData();
$data['sections'] = $survey->tree();

//@TODO add authorization check here
$screen = new Screen('sSurver','update');
//@TODO add breadcrumb
$screen->pageTitle(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_UPDATE));
$screen->toolbar()
		->add(new Button('back',Generator::url('survey','search')))->parentObject()
		->add(new Button('test',Generator::url('survey','test',null,array('surveyid' => $survey->surveyid)),
					Msg(Messages::BUNDLE,Messages::TEST)))->parentObject()
		->add(new Button('copy',Generator::url('survey','copy',null,array('surveyid' => $survey->surveyid)),
					Msg(Messages::BUNDLE,Messages::SURVEY_COPY)))
					->color(Button::COLOR_INFO)->parentObject()
		->add(new Button('delete','?'))
				->color(Button::COLOR_RED)
				->behaviors(Behaviors::action(
					Generator::url('survey','delete',null,array('inlineid' =>$survey->getInLineId())),
					true
				));
				
// Header Block
$blHeader = $xview->component('survey','header');
$blHeader->item('language')->input(false);
$screen->add($blHeader);
// Text Data 
$blTexts = $xview->component('survey','surveyTexts');
$screen->add($blTexts)->collapsed(true);
// Sections part
$gdSections = $xview->module(Generator::url('survey','sections',null,
						array('surveyid' => $survey->getId())));
$blSection = new Block('bSections',Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_SECTIONS));
$blSection->add($gdSections);
$screen->add($blSection)->collapsed(false);

$screen->initialize($data);

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		try {
			// Rebuild Tree
			if(is_array($fields['sections'])) {
				$changed = false;
				$sections = $questions = array();
				foreach($fields['sections'] as $treeData) {
					if($treeData['changed']) {
						$changed = true;
					}
					if(preg_match('/\:/',$treeData['id'])) {
						$sections[$treeData['id']] = array('position' => $treeData['position']);
					} else {
						$questions[$treeData['id']] = array('position' => $treeData['position'],'sectionid' => $treeData['sectionid']);
					}
				}
				if($changed) {
					foreach($sections as $inlineid => $changedData) {
						$section = $survey->findSections($inlineid);
						$section->setFields($changedData);
						$survey->replaceSections($section);
					}
					foreach($questions as $inlineid => $changedData) {
						$question = \Fox\Dao::load(Question::className(),$inlineid);
						$question->setFields($changedData);
						$question->prepareUpdate(Question::CHANGE_DEFAULT);
						$question->update();
					}
				}
			}
			$survey->setFields($fields);
			$survey->prepareUpdate(Survey::CHANGE_DEFAULT);
			if($survey->update() || $changed) {
				Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,$survey->label()));
				$screen->redirect();
			} else {
				Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_NO_UPDATE));
			}
		} catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		} catch (Fox\Object\Exception\NotExist $e) {
			Fox\Message::Error($e);
		}
	}
}
Fox\Ressources::instance()->register(new Script(_MODULES_.'survey'.DIRECTORY_SEPARATOR.'sections.js'));
echo Fox\Ressources::instance()->dump(true);
echo $screen->display();